package graph.searchers;

import graph.adapters.IAdapter;
import graph.filters.IFilter;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class Searcher<E, R, T> implements ISearcher<E, R, T> {
	
	private List<R> _foundElements = new LinkedList<R>();

	@Override
	public List<R> search(Iterator<T> iterator, IFilter<E, T> filter, IAdapter<R, T> adapter) {
		if (iterator == null || filter == null || adapter == null)
			return null;
		_foundElements.clear();
		while (iterator.hasNext()) {
			T currentElement = iterator.next();
			if (filter.check(currentElement))
				_foundElements.add(adapter.convert(currentElement));	
		}
		return new LinkedList<R>(_foundElements);
	}	

}
